package org.jash.document.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.coroutines.launch
import org.jash.common.adapter.CommonAdapter
import org.jash.common.log.logDebug
import org.jash.common.mvi.BaseActivity
import org.jash.common.mvi.BaseState
import org.jash.document.BR
import org.jash.document.R
import org.jash.document.databinding.ActivityDocBinding
import org.jash.document.entity.FileEntity
import org.jash.document.model.DocIntent
import org.jash.document.model.DocState
import org.jash.document.model.DocViewModel

@Route(path = "/document/doc")
class DocActivity : BaseActivity<ActivityDocBinding, DocViewModel>() {
    val adapter by lazy { CommonAdapter<FileEntity>({R.layout.item_file to BR.file}) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.recycler.adapter = adapter
        lifecycleScope.launch {
            viewModel.intent.send(DocIntent.SelDocuments(1, 1))
        }
    }
    fun loaded(res:DocState.Documents) {
        adapter += res.data
    }
    fun error(error:BaseState.Error) {
        logDebug(error.msg)
        Toast.makeText(this, error.msg, Toast.LENGTH_SHORT).show()
    }
    fun delete(del :DocState.Delete) {
        adapter.data.find { it.fileId == del.fileId }?.let {
            adapter -= it
            Toast.makeText(this, "${it.fileName} 被删除了", Toast.LENGTH_SHORT).show()
        }
    }
}